home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / GetPPPStatus / GetPPPStatus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.3 KB  |  314 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        GetPPPStatus.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "AEHelpers.h"
  25. #include <ASRegistry.h>
  26. #include <AppleEvents.h>
  27. #include <Fonts.h>
  28. #include <Events.h>
  29. #include <Files.h>
  30. #include <StandardFile.h>
  31. #include <Types.h>
  32. #include <Gestalt.h>
  33.  
  34. #include <stdio.h>
  35. #include <String.h>
  36. #include <stdlib.h>
  37. #include <Events.h>
  38. #include "OpenTptPPP.h"
  39. #include "MoreFinderEvents.h"
  40. #include "RAScriptingAdditions.h"
  41.  
  42. #define kFinderType         'FNDR'
  43. #define kFinderCreator        'MACS'
  44.  
  45. //************** Global definitions *********************//
  46. AEEventID    gPPPEventID;
  47.     
  48. //****************** Prototypes *************************//
  49. Boolean    NetInit(void);
  50. pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn);
  51. void Initialize(void);
  52. void ShowRAStatusResults(AppleEvent *aevent);
  53.  
  54. Boolean NetInit()
  55. {
  56.     OSStatus err;
  57.     UInt32 gResult;
  58.  
  59.         // determine whether OT/PPP or ARA 3.0 is present
  60.     err = Gestalt(gestaltOpenTptRemoteAccess, (long*) &gResult);
  61.     
  62.     if (err || (gResult == 0))
  63.     {
  64.         fprintf(stderr, "\nThis sample requires OT/PPP 1.0 or ARA 3.0");
  65.         return false;
  66.     }
  67.     
  68.         // if gestaltOpenTptRemoteAccess is present, determine whether OT/PPP or ARA 3.0 is present
  69.  
  70.     err = Gestalt(gestaltOpenTptRemoteAccessVersion, (long*) &gResult);
  71.     if (err)
  72.     {
  73.         fprintf(stderr, "\nThere was a problem determining the OT/PPP version present %ld", err);
  74.         return false;
  75.     }
  76.     
  77.     if (gResult >= kARA30Version)
  78.         gPPPEventID = kARAStatusRequestID;    // ARA 3.0 is present
  79.     else if (gResult != 0)
  80.         gPPPEventID = kPPPStatusRequestID;
  81.     else
  82.     {
  83.         fprintf(stderr, "\nThere was a problem determining the OT/PPP version present");
  84.         return false;
  85.     }
  86.     
  87.     return true;
  88. }
  89.  
  90.  
  91. void Initialize(void)
  92. {
  93.     /* Initialize all the needed managers. */
  94.     InitGraf(&qd.thePort);
  95.     InitFonts();
  96.     InitWindows();
  97.     InitMenus();
  98.     TEInit();
  99.     InitDialogs(nil);
  100.     InitCursor();
  101. }
  102.  
  103. void main(void)
  104. {
  105.     AppleEvent            reqEvent = { typeNull, nil };
  106.     AppleEvent            retEvent = { typeNull, nil };
  107.     AEDesc                firstRec = { typeNull, nil };
  108.     AEDesc                aeRec = { typeNull, nil };
  109.     AEDesc                baudRateDesc = { typeNull, nil };
  110.     AEDescList            aelist;
  111.     AERecord            aerecord;
  112.     AEIdleUPP            idleUPP;
  113.     OSErr                err;
  114.     
  115.     Initialize();
  116.  
  117.     if (NetInit() == false)
  118.         return;
  119.     
  120.         // set up the Finder as the target of the OT/PPP or ARA Apple Event call
  121.     err = AEHMakeAppleEventSignatureTarget(kFinderType, kFinderCreator, kNetworkAccessClass,
  122.                                             gPPPEventID, &reqEvent);
  123.     
  124.     if (err != noErr)
  125.     {
  126.         printf("Error calling AEHMakeAppleEventSignatureTarget: %d", err);
  127.         return;
  128.     }        
  129.  
  130.         // send the Apple event to the Finder for processing.
  131.     idleUPP = NewAEIdleProc(MyIdleFunction);
  132.     err = AESend(&reqEvent, &retEvent, kAEWaitReply+kAENeverInteract, kAENormalPriority,
  133.                     kAEDefaultTimeout, idleUPP, nil);
  134.                     
  135.     if (err != noErr)
  136.     {
  137.         printf("Error calling AESend = %d", err);
  138.     }
  139.     else
  140.     {
  141.             // get the apple event object
  142.         err = AEGetParamDesc(&retEvent, keyDirectObject, typeAEList, &aelist);
  143.         if (err == noErr)
  144.         {
  145.                 // get the apple event record 
  146.             err = AEGetParamDesc(&aelist, typeAERecord, typeAERecord, &aerecord);
  147.             
  148.             if (err == noErr)
  149.             {
  150.                     // call the function that will parse the return info
  151.                 ShowRAStatusResults(&aerecord);
  152.             }
  153.             else
  154.             {
  155.                 printf("Error calling AEGetParamDesc to get aerecord %d", err);
  156.             }
  157.             
  158.             AEDisposeDesc(&aelist);
  159.             
  160.         }
  161.         else
  162.             printf("Error calling AEGetParamDesc to getaelist %d", err);
  163.  
  164.         
  165.         AEDisposeDesc(&retEvent);
  166.     }
  167.     
  168.     AEDisposeDesc(&reqEvent);
  169. }
  170.  
  171. pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn)
  172. {
  173.     #pragma unused(event,sleep,mousergn)
  174.     Boolean    result = false;
  175.     
  176. //    DebugStr("\pIn idle function");
  177.     
  178.     return result;
  179. }        
  180.  
  181. void ShowRAStatusResults(AppleEvent *aerecord)
  182. {
  183.     AEDesc                desc = { typeNull, nil };
  184.     UInt32                *l;
  185.     Size                size;
  186.     OSErr                err;
  187.     char                str[256];
  188.     
  189.     err = AEGetParamDesc(aerecord, kRAStateKey, typeChar, &desc);
  190.     if (err == noErr)
  191.     {
  192.         size = GetHandleSize(desc.dataHandle);
  193.         if (size != 0)
  194.         {
  195.                 // get the connect state into a local string
  196.             BlockMove(*(desc.dataHandle), str, size);
  197.             str[size] = 0;
  198.             printf("\nConnection State - %s", str);
  199.         }
  200.         AEDisposeDesc(&desc);
  201.     }
  202.     else
  203.     {
  204.         printf("\nError calling AEGetParamDesc to get connection: %d", err);
  205.         return;
  206.     }
  207.  
  208.     err = AEGetParamDesc(aerecord, kRASecsConnectKey, typeInteger, &desc);
  209.     if (err == noErr)
  210.     {
  211.         l = (UInt32*)*desc.dataHandle;
  212.         printf("\nSeconds Connected - %ld", *l);
  213.         AEDisposeDesc(&desc);
  214.     }
  215.     else
  216.     {
  217.         printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
  218.     }
  219.     
  220.     err = AEGetParamDesc(aerecord, kRASecsRemainKey, typeInteger, &desc);
  221.     if (err == noErr)
  222.     {
  223.         l = (UInt32*)*desc.dataHandle;
  224.         printf("\nSeconds Reamining - %ld", *l);
  225.         AEDisposeDesc(&desc);
  226.     }
  227.     else
  228.     {
  229.         printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
  230.     }
  231.  
  232.     err = AEGetParamDesc(aerecord, kRAMessageKey, typeChar, &desc);
  233.     if (err == noErr)
  234.     {
  235.         size = GetHandleSize(desc.dataHandle);
  236.         if (size != 0)
  237.         {
  238.                 // get the message text into a local string
  239.             BlockMove(*(desc.dataHandle), str, size);
  240.             str[size] = 0;
  241.             printf("\nConnection Message - %s", str);
  242.         }
  243.         AEDisposeDesc(&desc);
  244.     }
  245.     else
  246.     {
  247.         printf("\nError calling AEGetParamDesc to get the message: %d", err);
  248.     }
  249.  
  250.     if (gPPPEventID >= kARAStatusRequestID)  // to check for protocol, check if ARA 3.0 is present
  251.     {
  252.         err = AEGetParamDesc(aerecord, kRAProtocolKey, typeChar, &desc);
  253.         if (err == noErr)
  254.         {
  255.             size = GetHandleSize(desc.dataHandle);
  256.             if (size != 0)
  257.             {
  258.                     // get the protocol type text into a local string
  259.                 BlockMove(*(desc.dataHandle), str, size);
  260.                 str[size] = 0;
  261.                 printf("\nProtocol - %s", str);
  262.             }
  263.             AEDisposeDesc(&desc);
  264.         }
  265.         else
  266.         {
  267.             printf("\nError calling AEGetParamDesc to get protocol: %d", err);
  268.         }
  269.     }
  270.     
  271.     err = AEGetParamDesc(aerecord, kBaudRateKey, typeChar, &desc);
  272.     if (err == noErr)
  273.     {
  274.         size = GetHandleSize(desc.dataHandle);
  275.         if (size != 0)
  276.         {
  277.                 // get the baud rate text into a local string
  278.             BlockMove(*(desc.dataHandle), str, size);
  279.             str[size] = 0;
  280.             printf("\Baud Rate - %s", str);
  281.         }
  282.         AEDisposeDesc(&desc);
  283.     }
  284.     else
  285.     {
  286.         printf("\nError calling AEGetParamDesc to get baud rate: %d", err);
  287.     }
  288.     
  289.     err = AEGetParamDesc(aerecord, kRABytesInKey, typeInteger, &desc);
  290.     if (err == noErr)
  291.     {
  292.         l = (UInt32*)*desc.dataHandle;
  293.         printf("\nNum Bytes In - %ld", *l);
  294.         AEDisposeDesc(&desc);
  295.     }
  296.     else
  297.     {
  298.         printf("\nError calling AEGetParamDesc to get the number of bytes in: %d", err);
  299.     }
  300.     
  301.     err = AEGetParamDesc(aerecord, kRABytesOutKey, typeInteger, &desc);
  302.     if (err == noErr)
  303.     {
  304.         l = (UInt32*)*desc.dataHandle;
  305.         printf("\nBytes Out - %ld", *l);
  306.         AEDisposeDesc(&desc);
  307.     }
  308.     else
  309.     {
  310.         printf("\nError calling AEGetParamDesc to get the number of bytes out: %d", err);
  311.     }
  312.     
  313. }    
  314.